package ui.swing.tree;

import java.awt.Font;
import java.util.Enumeration;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.UIManager;
import javax.swing.tree.DefaultMutableTreeNode;

public class TreeFromTreeNodeDemo extends JFrame {

	private static final long serialVersionUID = -278034040905550629L;

	public TreeFromTreeNodeDemo() {
		super("用TreeNode建树");
		setBounds(200,200,300,400);

		Font font = new Font("Dialog", Font.PLAIN, 12);
		Enumeration keys = UIManager.getLookAndFeelDefaults().keys();
		/** 定义Windows风格界面 **/
		while (keys.hasMoreElements()) {
			Object key = keys.nextElement();
			if (UIManager.get(key) instanceof Font) {
				UIManager.put(key, font);
			}
		}
		try {
			UIManager
					.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
		} catch (Exception el) {
			System.exit(0);
		}
		
		DefaultMutableTreeNode root = new DefaultMutableTreeNode("资源管理器");
		DefaultMutableTreeNode node1=new DefaultMutableTreeNode("我的公文包");
		DefaultMutableTreeNode node2=new DefaultMutableTreeNode("我的电脑");
		DefaultMutableTreeNode node3=new DefaultMutableTreeNode("网站");
		DefaultMutableTreeNode node4=new DefaultMutableTreeNode("Readme");
		root.add(node1);
		root.add(node2);
		root.add(node3);
		root.add(node4);

		DefaultMutableTreeNode leafNode = new DefaultMutableTreeNode("公司文件");
		node1.add(leafNode);
		leafNode = new DefaultMutableTreeNode("私人文件");
		node1.add(leafNode);
		leafNode = new DefaultMutableTreeNode("个人信件");
		node1.add(leafNode);
		
		leafNode = new DefaultMutableTreeNode("本机磁盘(c:)");
		node2.add(leafNode);
		leafNode = new DefaultMutableTreeNode("本机磁盘(d:)");
		node2.add(leafNode);
		leafNode = new DefaultMutableTreeNode("本机磁盘(e:)");
		node2.add(leafNode);
		
		leafNode = new DefaultMutableTreeNode("搜索");
		leafNode.add(new DefaultMutableTreeNode("百度"));
		leafNode.add(new DefaultMutableTreeNode("Google"));
		leafNode.add(new DefaultMutableTreeNode("必应"));
		node3.add(leafNode);
		leafNode = new DefaultMutableTreeNode("门户");
		leafNode.add(new DefaultMutableTreeNode("新浪"));
		leafNode.add(new DefaultMutableTreeNode("搜狐"));
		leafNode.add(new DefaultMutableTreeNode("网易"));
		node3.add(leafNode);
		
		JTree tree = new JTree(root);
		JScrollPane jsp = new JScrollPane();
		jsp.setViewportView(tree);
		
		getContentPane().add(jsp);
		pack();
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setVisible(true);
	}
	
	public static void main(String[] args) {
		new TreeFromTreeNodeDemo();
	}
	
}
